﻿using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Sustainalytics.Entities;
using System;
using System.Linq;

namespace Sustainalytics.DataLayer
{
    public partial class CompanyScreeningRepository
    {
        public CriteriaLevel1 GetCriteriaLevelOne(Guid criteriaLevel1Id)
        {
            var coll = _clientMongoDb.GetCollection<CriteriaLevel1>(typeof(CriteriaLevel1).Name);

            var filter = FilterDefinition<CriteriaLevel1>.Empty;
            if (criteriaLevel1Id != Guid.Empty)
                filter = Builders<CriteriaLevel1>.Filter.Where(c => c.Id == criteriaLevel1Id);

            return coll.Find(filter).FirstOrDefault();
        }

        public CriteriaLevel2 GetCriteriaLevelTwo(Guid criteriaLevel2Id)
        {
            var coll = _clientMongoDb.GetCollection<CriteriaLevel2>(typeof(CriteriaLevel2).Name);

            var filter = FilterDefinition<CriteriaLevel2>.Empty;
            if (criteriaLevel2Id != Guid.Empty)
                filter = Builders<CriteriaLevel2>.Filter.Where(c => c.Id == criteriaLevel2Id);

            return coll.Find(filter).FirstOrDefault();
        }

        public CriteriaLevel2 GetCriteriaLevelTwoByLevel3Id(Guid criteriaLevel3Id)
        {
            var colL3 = _clientMongoDb.GetCollection<CriteriaLevel3>(typeof(CriteriaLevel3).Name);

            var query = colL3.AsQueryable();
            if (criteriaLevel3Id != Guid.Empty)
                query = query.Where(c => c.Id == criteriaLevel3Id);
            var level3 = query.FirstOrDefault();

            var coll = _clientMongoDb.GetCollection<CriteriaLevel2>(typeof(CriteriaLevel2).Name);

            var query2 = coll.AsQueryable();
            if (level3.CriteriaLevel2Id != Guid.Empty)
                query2 = query2.Where(c => c.Id == level3.CriteriaLevel2Id);

            return query2.FirstOrDefault();
        }

        public CriteriaLevel3 GetCriteriaLevelThree(Guid criteriaLevel3Id)
        {
            var coll = _clientMongoDb.GetCollection<CriteriaLevel3>(typeof(CriteriaLevel3).Name);

            var query = coll.AsQueryable();
            if (criteriaLevel3Id != Guid.Empty)
                query = query.Where(c => c.Id == criteriaLevel3Id);

            return query.FirstOrDefault();
        }
    }
}
